package com.whlilong.jxc.sys;


import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.hibernate.HibernateException;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.db.HibernateSessionFactory;
import com.whlilong.jxc.model.Users;
import com.whlilong.jxc.ui.LoginDialog;


public class Login {

	boolean isSuccessful = false;

	public Login() {
		// TODO Auto-generated constructor stub
	}

	public boolean isLogin() {

		final LoginDialog loginDialog = new LoginDialog(Display.getCurrent()
				.getActiveShell());

		while (!isSuccessful) {
			if (loginDialog.open() != Window.OK)
				return false;

			String username = SaveLogInfo.getInstance().getUsername();
			String password = SaveLogInfo.getInstance().getPassword();

			isSuccessful = isValidate(username, password);
			if (!isSuccessful) {
				MessageDialog.openWarning(null, "提示", "用户名或密码错误，无法登录！");
			}

		}
		return isSuccessful;
	}

	private boolean isValidate(final String username, final String password) {
		// TODO Auto-generated method stub

		// create the login dialog
		ProgressMonitorDialog pmd = new ProgressMonitorDialog(null);
		IRunnableWithProgress rwp = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				// excute the login
				isSuccessful = exeLogin(monitor, username, password);
			}

		};
		try {
			pmd.run(true, false, rwp); // display the login progress dialog

		} catch (Exception e) {
			isSuccessful = false;
		}
		return isSuccessful;
	}

	private boolean exeLogin(final IProgressMonitor monitor, String username,
			String password) {
		// TODO Auto-generated method stub
		boolean tag = false;
		try {
			monitor.beginTask("正在登录数据库，请稍候。。。", IProgressMonitor.UNKNOWN);
			
			monitor.subTask("创建Hibernate工厂。。。");
			
//			Session session = HibernateSessionFactory
//					.getSession("hibernate_derby.cfg.xml");
			
			monitor.subTask("验证用户名、密码。。。");
			int[] buffer={0,1};
			Object[] object={username,password};
			List result = (List)DbManager.getInstance().search("com.whlilong.jxc.sys.Login.exeLogin", buffer, object);
//			Query query = session.getNamedQuery("com.whlilong.jxc.sys.Login.exeLogin");
//			query.setString(0, username);
//			query.setString(1, password);
//			Iterator iterator = query.iterate();
			if (result.size()>0) {
				Users user = (Users)result.get(0);
				SaveLogInfo.getInstance().setUsertag(user.getUsertag());
				tag = true;
			}
			monitor.subTask("销毁Hibernate Session 。。。");
//			HibernateSessionFactory.closeSession();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			tag = false;
		}finally{
			monitor.done();
		}
		return tag;
		
	}

}
